var express = require('express');
var router = express.Router();
let {Xiche,Xichetype,Xichedengji,Xihejilu,xihebaobiao} =require('../model/model')

/* GET home page. */
// 添加洗盒登记
router.post('/xicheadd',(req,res)=>{
    Xiche.create(req.body)
    res.send({
        code:'200',
    })
})
// 添加车辆编号
router.post('/xichetypeadd',(req,res)=>{
    Xichetype.create(req.body)
    res.send({
        code:'200'
    })
})

// 洗车登记数据
router.get('/getdengji',async(req,res)=>{
    let {page}=req.query;
    let datas=await Xiche.find({yuey:false}).populate('carid').skip((page-1)*10).limit(10)
    let total=(await Xiche.find()).length
    let tylist=await Xichetype.find()
    res.send({
        code:'200',
        list:datas,
        total,
        tylist
    })
})

router.get('/getdengjitow',async(req,res)=>{
    let {page}=req.query;
    let datas=await Xiche.find({yuey:true}).populate('carid').skip((page-1)*10).limit(10)
    let total=(await Xiche.find()).length
    res.send({
        code:'200',
        list:datas,
        total
    })
})

router.post('/addXichedengji',(req,res)=>{
    Xichedengji.create(req.body)
    res.send({
        code:'200',
    })
})
router.get('/xihelogin',async(req,res)=>{
    let {_id,sjname,start,end}=req.query
    let add=[{}]
    if(sjname){
        add.push({sjname:new RegExp(sjname)})
    }
    if(_id){
        add.push({_id:_id})
    }
    let datas=await Xichedengji.find({$and:add})
    let total=datas.length
    res.send({
        code:'200',
        list:datas,
        total
    })
})

router.post('/upyuyue',async(req,res)=>{
    let {id}=req.body
    let oo=await Xiche.find({_id:id})
    await Xiche.updateOne({_id:id},{yuey:!oo[0]['yuey']})

    res.send({
        code:'200',
    })
})

router.post('/delsome',async(req,res)=>{
    let {ids}=req.body
    await Xiche.deleteOne({_id:ids})
    res.send({
        code:'200'
    })
})

router.post('/addXihejilu',(req,res)=>{
    Xihejilu.create(req.body)
    res.send({
        code:'200',
    })
})

router.get('/getXihejilu',async(req,res)=>{
    let {name}=req.query
    let ass=[{}]
    if(name){
        ass.push({name:new RegExp(name)})
    }    
    let datas=await Xihejilu.find({$and:ass})
    let total=datas.length
    res.send({
        code:'200',
        list:datas,
        total
    })
})

router.post('/xihebaobiao',(req,res)=>{
    xihebaobiao.create(req.body)
    res.send({
        code:'200',
    })
})

router.get('/xihelist',async(req,res)=>{
    let {page}=req.query
    let datas=await xihebaobiao.find().skip((page-1)*10).limit(10)
    let total=(await xihebaobiao.find()).length
    res.send({
        code:'200',
        list:datas,
        total
    })
})
module.exports = router;